"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.EcsEntityGroup = void 0;
function _sortEntityById(a, b) {
    if (a.id < b.id)
        return -1;
    else
        return 1;
}
var EcsEntityGroup = /** @class */ (function () {
    function EcsEntityGroup(compIds, entities) {
        this.entities = [];
        this.compIds = [];
        this.compIdMap = {};
        this.indexMap = {};
        this.compIds = compIds;
        for (var i = 0; i < compIds.length; i++) {
            this.compIdMap[compIds[i]] = true;
        }
        for (var k in entities) {
            var _entity = entities[k];
            var _compsID = _entity.groupId;
            if (_entity.hasAllCompIds.apply(_entity, compIds)) {
                this.entities.push(_entity);
            }
        }
        this.entities.sort(_sortEntityById);
        for (var index = 0; index < this.entities.length; index++) {
            var entity = this.entities[index];
            this.indexMap[entity.id] = index;
        }
    }
    EcsEntityGroup.prototype.hasEntity = function (entity) {
        return this.indexMap[entity.id] != undefined;
    };
    /**从entites列表中最后一个元素向前遍历，防止删除列表元素时的错位问题 */
    EcsEntityGroup.prototype.forEach = function (callbackfn, thisArg) {
        if (!thisArg)
            thisArg = this;
        for (var index = this.entities.length - 1; index >= 0; index--) {
            var element = this.entities[index];
            callbackfn.call(thisArg, element, index, this.entities);
        }
    };
    EcsEntityGroup.prototype.add = function (entity) {
        this.entities.push(entity);
        this.indexMap[entity.id] = this.entities.length - 1;
    };
    EcsEntityGroup.prototype.remove = function (entity) {
        var index = this.indexMap[entity.id];
        if (index || index == 0) {
            delete this.indexMap[entity.id];
            this.entities[index] = this.entities[this.entities.length - 1];
            var switchEcsEntity = this.entities[index];
            if (switchEcsEntity !== entity) {
                this.indexMap[switchEcsEntity.id] = index;
            }
            this.entities.length -= 1;
        }
    };
    EcsEntityGroup.prototype.isMatch = function (entity) {
        for (var i = 0; i < this.compIds.length; i++) {
            if (!entity.componentMap.has(this.compIds[i])) {
                return false;
            }
        }
        return true;
    };
    return EcsEntityGroup;
}());
exports.EcsEntityGroup = EcsEntityGroup;
